﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FSY.Components.DataProvider;
using FSY.API.CacheManager;
using FSY.API.Utilities;
using FSY.Globals;
namespace FSY.Components.Entities
{
    public class Fsy_MenuController
    {
        private static int InsertDeleteUpdate(Fsy_Menu menu, DataProviderAction action)
        {
            Fsy_Cache.RemoveCache(cachKeyMenuAll);          
            return Fsy_DataProvider.Instance.Menu_AddDeleteUpdate(menu, action);
        }
        public static int Insert(Fsy_Menu menu)
        {
            return InsertDeleteUpdate(menu, DataProviderAction.Insert);
        }
        public static int Delete(int ID)
        {
            Fsy_Menu menu = new Fsy_Menu();
            menu.MenuID = ID;
            return InsertDeleteUpdate(menu, DataProviderAction.Delete);
        }
        public static int Update(Fsy_Menu menuModule)
        {
            return InsertDeleteUpdate(menuModule, DataProviderAction.Update);
        }
        static string cachKeyMenuAll = "MenuAll";
        private static object objlock = new object();
        public static List<Fsy_Menu> GetAllMenu()
        {            
            List<Fsy_Menu> lsMenuModule = new List<Fsy_Menu>();
            lock (objlock)
            {
                lsMenuModule = (List<Fsy_Menu>)Fsy_Cache.GetCache(cachKeyMenuAll);
                if (lsMenuModule == null)
                {
                    lsMenuModule = Fsy_DataProvider.Instance.Menu_GetListAll();
                    Fsy_Cache.SetCache(cachKeyMenuAll, lsMenuModule, Fsy_Config.SlidingExpiration());
                    return lsMenuModule;
                }
            }
            return lsMenuModule;
        }
        public static Fsy_Menu GetMenuByID(int menuID)
        {
            List<Fsy_Menu> ls = GetAllMenu();
            foreach (Fsy_Menu menu in ls)
            {
                if (menu.MenuID == menuID)
                {
                    return menu;
                }
            }
            return null;
        }
        public static bool IsAuthorization(int user, int role)
        {
            return false;
        }
    }
}
